<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0; width: 100%;display:flex">

        <el-button type="primary" icon="el-icon-arrow-left" @click="back">返回</el-button>
        <p class="count">{{ this.form.name }}成绩</p>
    </div>
    <div class="div-header">
      <el-card class="card-header">
        <div
          id="myChart"
          :style="{ width: '100%', height: '500px' }"
          ref="myChart"
        ></div>
      </el-card>
    </div>
  </div>
</template>
 
<script>
import request from "../../utils/request";
export default {
  name: "Index",
  data() {
    return {
      chartInstance: null,
      allData: null,
      for: {},
      course: {},
    };
  },
  created() {
    let strexam = sessionStorage.getItem("examInfo") || "{}";
    this.form = JSON.parse(strexam);
    let strcourse = sessionStorage.getItem("courseInfo") || "{}";
    this.course = JSON.parse(strcourse);
    console.log(this.form);
  },
  mounted() {
    this.initChart();
    this.getData();
  },
  methods: {
    // 返回成绩列表
    back(){
      this.$router.push("/teacherscorelist");
    },
    // 初始化echarts对象
    initChart() {
      this.chartInstance = this.$root.echarts.init(
        document.getElementById("myChart")
      );
    },
    // 获取数据
    getData() {
      request
        .get("/escore/test", {
          params: {
            examId: this.form.id,
            gradeId: this.form.gradeId,
            courseId: this.course.gradeList.courseList[0].id,
          },
        })
        .then((res) => {
          console.log("数据");
          console.log(res.data);
          this.allData = res.data;
          this.updateChart();
        });
    },
    // 更新图表
    updateChart() {
      const dataName = this.allData.map((item) => {
        return item.clazzName;
      });
      const dataValue = this.allData.map((item) => {
        return item.clazzAvg;
      });
      const option = {
        tooltip: {
          trigger: "item",
          axisPointer: {
            type: "shadow",
          },
        },
        title: {
          text: "各班平均分",
        },

        grid: {
          top: "20%",
          left: "3%",
          right: "4%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: dataName,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "平均分",
            type: "bar",
            barWidth: 66,
            data: dataValue,
            label: {
              show: true,
              position: "top",
            },
          },
        ],
      };
      this.chartInstance.setOption(option);
    },
  },
};
</script>

<style scoped>
.div-header {
  margin-top: 100px;
  margin-left: 15px;
  width: 100%;
  margin: 0 auto;
}

.card-header {
  margin-top: 50px;
  width: 100%;
  height: 500px;
}
.count {
  color: #999;
  font-size: 15px;
  font-weight: bold;
  text-align: center;
  width: 80%;
  height: 32px;
  line-height: 32px;
}
</style>
 